package demo_0711;

public class T12_0324 {
    public boolean isBalanced(TreeNode root) {
        return getHigh(root) >= 0;
    }
    private int getHigh(TreeNode root) {
        if (root == null) return 0;
        // 左树高度，及是否平衡
        int lefthigh = getHigh(root.left);
        // 右树高度，及是否平衡
        int righthigh = getHigh(root.right);
        // 本节点是否平衡 || 左节点是否平衡 || 右节点是否平衡
        if (Math.abs(lefthigh - righthigh) > 1 || lefthigh < 0 || righthigh < 0) {
            return -1;
        } else {
            return Math.max(lefthigh,righthigh) + 1;
        }
    }
}
